package com.android.lovingpet.community.sift;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.GuideAdapter;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.common.postlist.PostListAdapter;
import com.android.lovingpet.common.postlist.PostListContract;
import com.android.lovingpet.common.postlist.PostListFragment;
import com.android.lovingpet.common.postlist.PostListPresenter;
import com.android.lovingpet.community.category.CategoryActivity;
import com.android.lovingpet.community.teams.TeamsActivity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class SiftFragment extends BaseFragment<PostListContract.View, PostListPresenter> implements PostListContract.View, View.OnClickListener {

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private SiftAdapter adapter;
    private List<Post> posts;

    @Override
    protected int setContentView() {
        return R.layout.fragment_sift;
    }

    @Override
    protected void onCreateView(View view) {
        posts = new ArrayList<>();
        adapter = new SiftAdapter(posts, this);
        recyclerView = view.findViewById(R.id.rv_sift);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //recyclerView.setNestedScrollingEnabled(false);
        //recyclerView.setRefreshHeader(recyclerView.getDefaultRefreshHeaderView());
        recyclerView.setAdapter(adapter);
        /*recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getPresenter().getPosts(0, 0);
            }

            @Override
            public void onLoadMore() {
                getPresenter().getPosts(0, posts.size());
            }
        });*/
        refreshLayout = view.findViewById(R.id.refresh_layout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getPresenter().getPosts(0, 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getPresenter().getPosts(0, posts.size());
            }
        });
        getPresenter().getPosts(0, 0);
    }

    @Override
    public PostListPresenter createPresenter() {
        return new PostListPresenter(-1);
    }

    @Override
    public PostListContract.View createView() {
        return this;
    }

    @Override
    public void addPosts(List<Post> posts) {
        this.posts.addAll(posts);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearPosts() {
        posts.clear();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setUser() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void refresh() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.autoRefresh();
                //recyclerView.refresh();
            }
        });
    }

    @Override
    public void complete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                //recyclerView.refreshComplete();
                //recyclerView.loadMoreComplete();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_category:
                startActivity(new Intent(getContext(), CategoryActivity.class));
                break;

            case R.id.ll_teams:
                startActivity(new Intent(getContext(), TeamsActivity.class));
                break;

            default:
                break;
        }
    }
}
